package com.example.rbac.aop;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Aspect
public class Log {

    private Logger log = Logger.getLogger(this.getClass());

    @Pointcut("execution(* com.example.rbac.controller..*(..))")
    public void pointCutName(){}

    @Before("pointCutName()")
    public void doBeforeAdvice(JoinPoint joinPoint){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        log.info(request.getRequestURL().toString());
        log.info(request.getMethod());
        log.info(joinPoint.getSignature());
        log.info(joinPoint.getArgs());
    }



    @After(value = "pointCutName()")
    public void doAfterAdvice(JoinPoint joinPoint){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = requestAttributes.getResponse();
        log.info(response.getHeaderNames());
        log.info(response.getStatus());
        log.info( joinPoint.getSignature());
        log.info(joinPoint.getArgs());
    }
}
